package com.atguigu.order.service.impl;

import com.aiguigu.commonutils.JwtUtils;
import com.aiguigu.commonutils.ResultCode;
import com.atguigu.order.entity.Order;
import com.atguigu.order.feign.EduCourseService;
import com.atguigu.order.feign.UcenterService;
import com.atguigu.order.mapper.OrderMapper;
import com.atguigu.order.service.OrderService;
import com.atguigu.order.util.OrderNoUtil;
import com.atguigu.servicebase.entity.vo.CourseOrderVo;
import com.atguigu.servicebase.entity.vo.UcenterMemberVo;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-10-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    /**
     * redis服务
     */
    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 远程调用方法：用户服务
     */
    @Resource
    private UcenterService ucenterService;
    /**
     * 远程调用方法：课程服务
     */
    @Resource
    private EduCourseService  eduCourseService;



    @Override
    public String saveEntity(String courseId, String token) {
        Order order = new Order();
        String redisToken= (String)redisTemplate.opsForValue().get(token);
        // 获取 用户信息
        String userId = JwtUtils.getIdByToken(redisToken);
        if (StringUtils.isEmpty(userId)) {
            throw new GuLiException(ResultCode.NO_LOGIN, "未登录");
        }
        UcenterMemberVo ucenterMemberVo = ucenterService.getUserInfo(userId);
        // 获取 课程信息
        CourseOrderVo courseInfoOrder = eduCourseService.getCourseInfoOrder(courseId);
        // 生成订单号
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName("test");
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(ucenterMemberVo.getId());
        order.setMobile(ucenterMemberVo.getMobile());
        order.setNickname(ucenterMemberVo.getNickname());
        // 订单状态：0=未支付，1=已支付
        order.setStatus(0);
        // 支付类型：1=微信
        order.setPayType(1);
        baseMapper.insert(order);
        // 返回订单号
        return order.getOrderNo();
    }

    @Override
    public Boolean getCourseOrderInfoByCourseId(String courseId, String token) {
        Order order = new Order();
        String redisToken= (String)redisTemplate.opsForValue().get(token);
        // 获取 用户信息
        String userId = JwtUtils.getIdByToken(redisToken);
        if (StringUtils.isEmpty(userId)) {
            throw new GuLiException(ResultCode.NO_LOGIN, "未登录");
        }
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Order::getCourseId,courseId)
                .eq(Order::getMemberId,userId)
                .eq(Order::getStatus,1)
                .orderByDesc(Order::getGmtCreate)
        ;
        Order one = this.getOne(queryWrapper);


        return Objects.isNull(one) ? false : true ;
    }
}
